import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Reguser from '@/components/Reguser.vue'
import Userinfo from '@/components/Userinfo.vue'
import Updatepwd from '@/components/Updatepwd.vue'
import Avatar from '@/components/Avatar.vue'
import Index from '@/views/index.vue'
import Order from '@/components/Order.vue'
import Worker from '@/components/Worker.vue'
import Sales from '@/components/Sales.vue'
import ReturnOrder from '@/components/ReturnOrder.vue'
import Errors from '@/components/404.vue'
// vuex
import store from '@/store/index'


Vue.use(VueRouter)

// 动态路由 路由规则
const userinfoRule = {path: '/userinfo', component: Userinfo, name: 'userinfo'}
// const userinfoRule = {path: '/userinfo', component: () => import('@/components/Userinfo.vue'), name: 'userinfo'}
const updatepwdRule = {path: '/updatepwd', component: Updatepwd , name: 'updatepwd'}
const updateAvatarRule = {path: '/updateAvatar', component: Avatar, name: 'updateAvatar'}
const orderRule = {path: '/order', component: Order, name: 'order'}
const workerRule = {path: '/worker', component: Worker, name: 'worker'}
const salesRule = {path: '/sales', component: Sales, name: 'sales'}
const returnOrderRule = {path: '/returnOrder', component: ReturnOrder, name: 'returnOrder'}

// 路由映射规则
const ruleMapper = {
  userinfo: userinfoRule,
  updatepwd: updatepwdRule,
  updateAvatar: updateAvatarRule,
  order: orderRule,
  worker: workerRule,
  sales: salesRule,
  returnOrder: returnOrderRule
}

const router = new VueRouter({
  routes: [
    {path: '/', redirect: '/login'},
    {path: '/login', component: Login},
    {path: '/reguser', component: Reguser},
    {
      path: '/home',
      redirect: '/index',
      component: Home,
      children: [
        {path: '/index', component: Index},
      ]
    },
    // {path: '*', redirect: '/404'},
    // {path: '/404', component: Errors},
  ]
})

 

// 解决重复点击this.$router.push/replace报错问题
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function(location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}

VueRouter.prototype.replace = function(location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject)
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}

export function initDynamicRouter() {
  // 根据二级权限,对路由规则进行动态添加
  const currentRoutes = router.options.routes
  // console.log(currentRoutes)
  const rightList = store.state.rightList
  rightList.forEach(item => {
    item.children.forEach(item => {
      // item 二级权限
      const temp = ruleMapper[item.path]
      temp.meta = item.rights
      currentRoutes[3].children.push(temp)
    })
  })
  currentRoutes.push({path: '/*', component: Errors})
  // console.log(currentRoutes)
  router.addRoutes(currentRoutes)
} 
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行   next('/login')强制跳转
  if (to.path === '/login' || to.path === '/reguser') return next()
  // 否则需要获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')// 为假强行跳转登录页
  // 为真则放行
  next()
}) 

export default router
